package com.bestv.search.engine.compass.extend;

import com.bestv.search.common.util.StringConst;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.ScoreDocComparator;
import org.apache.lucene.search.SortComparatorSource;

import java.io.IOException;
import java.util.Map;

public class CustomSortComparatorSource implements SortComparatorSource {
    private static final long serialVersionUID = -2259998868994301427L;

    private String[] tags;
    private Map<String, Integer> maxHitCountByTagsMap;
    private long headerId;

    public CustomSortComparatorSource(String[] tags, Map<String, Integer> maxHitCountByTagsMap) {
        this.tags = tags;
        this.maxHitCountByTagsMap = maxHitCountByTagsMap;
    }

    public CustomSortComparatorSource(long headerId) {
        this.headerId = headerId;
    }

    @Override
    public ScoreDocComparator newComparator(IndexReader reader, String field) throws IOException {
        if (StringConst.TAGS.equals(field)) {
            return new TagsMaxHitCountComparator(tags, maxHitCountByTagsMap);
        } else if (StringConst.INFOHEADERITEMS.equals(field)) {
            return new InfoHeaderItemOrderComparator(reader, headerId);
        }

        return null;
    }
}